package com.example.astronautcontact0123456789hwh1104;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class ContactSQLiteOpenHelper extends SQLiteOpenHelper {

    public ContactSQLiteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }
    public ContactSQLiteOpenHelper(Context context){
        super(context, "astronautContact.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS contacts(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, phone TEXT not null, email TEXT,organization TEXT, notes TEXT)");
        insertInitialData(sqLiteDatabase);

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
    private void insertInitialData(SQLiteDatabase sqLiteDatabase){
        AstronautContact contact = new AstronautContact("张三", "0123456789", "zhangsan@gmail.com", "gdpt","gdpt");
        ContentValues values = new ContentValues();
        values.put("name", contact.getName());
        values.put("phone", contact.getPhone());
        values.put("email", contact.getEmail());
        values.put("organization", contact.getOrganization());
        values.put("notes", contact.getNotes());
        sqLiteDatabase.insert("contacts", null, values);

    }
}
